﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using IterationLife.Extensions.EasyUI.Models;
using IterationLife.Extensions.Helpers;
using Util.Helpers;

namespace IterationLife.Extensions.EasyUI.Commons
{
    /// <summary>
    /// 在线HTML编辑器控件
    /// </summary>
    public class UeditorControl<T> : IUeditorControl<T>
    {
        /// <summary>
        /// 初始化在线HTML编辑器控件
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        public UeditorControl(HtmlHelper<T> helper)
        {
            _helper = helper;
        }

        /// <summary>
        /// HtmlHelper
        /// </summary>
        private readonly HtmlHelper<T> _helper;


        /// <summary>
        /// 编辑
        /// </summary>
        public MvcHtmlString Edit<TProperty>(Expression<Func<T, TProperty>> expression,string iframeId="ueditor", int width = 1024, int height = 400)
        {
            return _helper.Action(
                "Iframe",
                "Ueditor",
                new
                {
                    area = "Systems",
                    parameter = new UeditorParameter
                    {
                        Id = iframeId,
                        Width = width,
                        Height = height,
                        LabelName = Lambda.GetName(expression),
                        Content = LambdaHelper<T>.GetValue(expression, _helper)
                    }
                });
        }
    }
}
